﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfSnqkGasAnalysis.IData;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis.IData_impl
{
    /// <summary>
    /// 数据接口实现。气分析审核历史
    /// </summary>
    /// 创建时间：2022-12-7 21:36:04。作者：王浩力
    public sealed class Gas_check_historyDAL_impl : BaseDAL, IGas_check_historyDAL
    {
        /// <summary>
        /// 【事务】驳回。审核处理。
        /// </summary>
        /// <param name="gas_Analysis"></param>
        /// <returns></returns>
        /// 创建时间：2022-12-10 15:41:22。作者：王浩力
        public IResult AddCheckHandleTrasacation(Gas_analysis gas_Analysis, Gas_check_history history)
        {
            //修改o_step
            string sql1 = $"update  Gas_analysis  set  o_step={gas_Analysis.O_step}   where  id={gas_Analysis.Id} ;";

            //记录clean_gas_check_history
            Snowflake snowflake = new Snowflake(1, 1);
            string sql2 = $@"INSERT INTO gas_check_history (
                                  id,
                                  check_user_name,
                                  check_user_id,
                                  check_time,
                                  check_stat,
                                  reject_reason,
                                  gas_analysis_key
                              )
                              VALUES(
                                  {snowflake.NextId()},
                                  $check_user_name,
                                  $check_user_id,
                                  '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}',
                                  $check_stat,
                                  $reject_reason,
                                  $gas_analysis_key
                              );";
            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            keyValuePairs.Add("$check_user_name", history.Check_user_name);
            keyValuePairs.Add("$check_user_id", history.Check_user_id);
            //keyValuePairs.Add("$check_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            keyValuePairs.Add("$check_stat", history.Check_stat);
            keyValuePairs.Add("$reject_reason", history.Reject_reason);
            keyValuePairs.Add("$gas_analysis_key", history.Gas_analysis_key);

            string errorMsg = null;
            string sql = sql1 + sql2;
            int count = base.ExecSqlCmd(sql, keyValuePairs, c =>
            {
                return c.ExecuteNonQuery();
            }, exp =>
            {
                errorMsg = exp.Message;
            });
            if (count > 0)
            {
                return new ResultSuccess("审核处理成功");
            }
            return new Result("审核处理异常：" + errorMsg);
        }


        public Result Add(Gas_check_history check_History)
        {
            Snowflake snowflake = new Snowflake(1, 1);
            string sql = $@"INSERT INTO gas_check_history (
                                  id,
                                  check_user_name,
                                  check_user_id,
                                  check_time,
                                  check_stat,
                                  reject_reason,
                                  gas_analysis_key
                              )
                              VALUES(
                                  {snowflake.NextId()},
                                  $check_user_name,
                                  $check_user_id,
                                  '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}',
                                  $check_stat,
                                  $reject_reason,
                                  $gas_analysis_key
                              );";
            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            keyValuePairs.Add("$check_user_name", check_History.Check_user_name);
            keyValuePairs.Add("$check_user_id", check_History.Check_user_id);
            //keyValuePairs.Add("$check_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            keyValuePairs.Add("$check_stat", check_History.Check_stat);
            keyValuePairs.Add("$reject_reason", check_History.Reject_reason);
            keyValuePairs.Add("$gas_analysis_key", check_History.Gas_analysis_key);
            string msg = null;
            int count = base.ExecSqlCmd(sql, keyValuePairs, c =>
            {
                return c.ExecuteNonQuery();
            },
            exp => { msg = exp.Message; }
            );
            if (count > 0)
            {
                return new Result("新增成功") { Code = 200 };
            }
            return new Result("新增失败" + msg);
        }

        public Gas_check_history Get(long id)
        {
            string sql = "select   *  from  gas_check_history  where id=" + id;
            var model = base.ExecSqlCmd(sql, null, c =>
            {
                var reader = c.ExecuteReader();
                if (!reader.HasRows)
                {
                    return null;
                }
                Gas_check_history vm = new Gas_check_history();
                if (reader.Read())
                {
                    vm.Id = reader.GetInt64(0);
                    vm.Check_user_name = reader["check_user_name"].ToString();
                    vm.Check_user_id = Convert.ToInt64(reader["check_user_id"]);
                    vm.Check_time = reader["check_time"].ToString();
                    vm.Check_stat = reader["check_stat"] is DBNull ? (byte)0 : Convert.ToByte(reader["check_stat"]);
                    vm.Reject_reason = reader["reject_reason"].ToString();
                    vm.Gas_analysis_key = (long)reader["gas_analysis_key"].ToString().ToMyDecimal();
                }
                return vm;
            });
            return model;
        }

        /// <summary>
        /// 根据气分析主键，查询审核历史列表
        /// </summary>
        /// <param name="gas_analysis_key">气分析主键</param>
        /// <returns></returns>
        /// 创建时间：2022-12-7 21:59:37。作者：王浩力
        public List<Gas_check_history> GetList(long gas_analysis_key)
        {
            string sql = "select   *  from  gas_check_history  where  gas_analysis_key=" + gas_analysis_key;
            return base.ExecSqlCmd(sql, null, c =>
            {
                var reader = c.ExecuteReader();
                if (!reader.HasRows)
                {
                    return null;
                }
                List<Gas_check_history> list = new List<Gas_check_history>();
                while (reader.Read())
                {
                    Gas_check_history vm = new Gas_check_history();
                    vm.Id = reader.GetInt64(0);
                    vm.Check_user_name = reader["check_user_name"].ToString();
                    vm.Check_user_id = Convert.ToInt64(reader["check_user_id"]);
                    vm.Check_time = reader["check_time"].ToString();
                    vm.Check_stat = reader["check_stat"] is DBNull ? (byte)0 : Convert.ToByte(reader["check_stat"]);
                    vm.Reject_reason = reader["reject_reason"].ToString();
                    vm.Gas_analysis_key = (long)reader["gas_analysis_key"].ToString().ToMyDecimal();
                    list.Add(vm);
                }
                return list;
            });

        }
    }
}
